﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.party;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Constant;

namespace Pixysoft.Weblications.Sango.Command.party
{
    partial class SangoPartyHandler
    {
        public party_applyanswer party_applyanswer(string applyusercode, bool answer)
        {
            string userCode = SangoManager.Authentication();

            party_applyanswer response = ReflectionManager.CreatePojo<party_applyanswer>();
            response.Succeed = false;

            Entities.Usr_Partymember myMember = PartyDao.tryGetActiveMember(userCode);

            if (myMember == null)
            {
                response.Message = "不属于当前军团，无法审批";
                return response;
            }

            Entities.Bil_Partyapply applyBill = PartyDao.getApplyMember(applyusercode);

            if (!myMember.Partyprimarycode.Equals(applyBill.Partyprimarycode))
            {
                response.Message = "不在相同的军团，无法审批。";
                return response;
            }

            if (myMember.Titletypecode < PtyTitleSerialcodeCollection.POSITION_VICE_PRESIDENT)
            {
                response.Message = "权限不足，无法审批";
                return response;
            }

            Entities.Usr_Profile applyProfile = UsrProfileDao.getUsrProfile(applyusercode);

            Entities.Usr_Party applyParty = PartyDao.tryGetActiveParty(applyBill.Partyprimarycode);

            if (applyParty.Nationtypecode != applyProfile.Nationtypecode)
            {
                response.Message = "国籍不相同，无法加入";
                return response;
            }

            if (answer && applyParty.Membercount >= applyParty.Maxmembercount)
            {
                response.Message = "军团人数已满，无法加入";
                return response;
            }

            Entities.Usr_Property applyProperty = UsrPropertyDao.getProperty(applyusercode);

            Entities.Usr_Partymission applyMission = PartyDao.getMission(applyParty.Primarycode, PtyMissionTypecodeCollection.LEVELUP);

            IEntityNoebeTransaction transaction = SettingHelper.EntityTransaction;

            transaction.Begin(delegate()
            {
                if (answer)
                {
                    Entities.Usr_Partymember partyMember = PartyDao.joinParty(applyProfile, applyProperty, applyParty, applyMission, applyBill);

                    transaction.Insert(partyMember);
                    transaction.Update(applyParty);
                    transaction.Update(applyBill);
                }
                else
                {
                    PartyDao.refuseApply(applyProfile, applyBill);
                    transaction.Update(applyBill);
                }

                transaction.Commit();
            });

            response.Succeed = true;
            return response;
        }


    }
    public interface party_applyanswer : ISangoResponse
    {
    }
}
